<template>
  <router-view />
</template>

<script setup>
import {onBeforeUnmount, onMounted, onUnmounted, watch} from "vue";
import { useRouter } from "vue-router";
import { GetQueryValue } from "@/common/utils/GlobalUtils";
import { otherApi } from "@/api/login";
import { useStore } from "vuex";
import {ElMessageBox} from "element-plus";
import {extractTree, filterAsyncRouter} from "@/utils/createRoutes";
import _ from 'lodash'
const router = useRouter();
const store = useStore();

// 菜单
const menuList = [
    "",
    "/asset/home",
    "/tender/index",
    "/businessFinance/home",
    "/constructionProjectManage/console_page",
    "/investmentProjectManagement/cockpit",
    "/constructionProjectManage/projectmanagement/index"
];


/**
 * 判断路由是否存在
 * @param flattenArray
 * @param path
 * @return {*}
 */
const isPath = (flattenArray,path) => {
    for (let item of flattenArray){
        if(item.sourceType === 1 && item.path === path){
            return item.path;
        }
    }
}

const checking = () => {
  let query = decodeURI(window.location.search.substring(1));
  let cookie =
    query.split("&").length === 0
      ? ""
      : query.split("&")[0].split("cookie=").length === 0
      ? ""
      : query.split("&")[0].split("cookie=")[1];
  let loginName = GetQueryValue("loginName");
  let type = GetQueryValue("type");

    if (type==='11'){
        return window.open(`http://116.169.61.207:12011/dashboard/finance?cookie=${cookie}&loginName=${loginName}&type=${type}`);
    }

  /* 新增type参数，跳转不同的系统的首页
      type=1 -->资产
      type=2 -->招标
      type=3 -->业财
      type==4 -->建设项目
      type=5 -->投资
      type=6 -->项目统筹
      type=11 -->大数据
    */
  if (cookie && loginName && type) {
    let params = {
      cookie: cookie,
      loginName: loginName,
    };
    // 预防浏览器缓存非法调转页面
    store.commit('user_clear');
    store.commit("menu_clear");
    // localStorage.clear();
    otherApi.checking(params).then((res) => {
      const { code, data, msg } = res;
      if (res.code === '200') {
        if(!res.data.menus || (res.data.menus && res.data.menus.length===0)){
            return  ElMessageBox.alert('此账号无菜单权限,请联系管理员!', '警告', {
                confirmButtonText: 'OK',
                callback: (action) => {},
            });
        }
        // 组装路由
        let list =   filterAsyncRouter(res.data.menus);
        store.commit('SetLeftMenu',list);
        list.forEach(route =>{
            router.addRoute(route);
        });
        let  flattenArray = extractTree(res.data.menus,'children');
        const userId = data.userId;
        store.commit("SET_USERID", userId);
        const userName = data.userName;
        store.commit("SET_USERNAME", userName);
        const account = data.account;
        store.commit("SET_USERACCOUNT", account);
        const email = data.email;
        store.commit("SET_EMAIL", email);
        const phoneNumber = data.phoneNumber;
        store.commit("SET_PHNOE", phoneNumber);
        const departmentId = data.departmentId;
        store.commit("SET_DEPARTMENT_ID", departmentId);
        const departmentName = data.departmentName;
        store.commit("SET_DEPARTMENT", departmentName);
        const tokenObj = data.token;
        store.commit("SET_TOKEN", tokenObj);

         let isBool =  isPath(flattenArray, menuList[type]) !== undefined;
         if(!isBool){
             return  ElMessageBox.alert('跳转菜单不存在,请联系管理员!', '警告', {
                 confirmButtonText: 'OK',
                 callback: (action) => {},
             });
         }
        setTimeout(() => {
          router.push({ path: menuList[type] });
        }, 600);
      }
    });
  }
};

watch(
  () => router.currentRoute.value.path,
  (value) => {
    if (value === "/login") {
      checking();
    }
  }
);

onMounted(() => {
  document.getElementById("loading-page").style.display = "none";
});

// 解决 ElTable 自动宽度高度导致的「ResizeObserver loop limit exceeded」报错问题
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
  constructor(callback) {
    callback = _.debounce(callback, 16);
    super(callback);
  }
};
</script>

<style>
#app {
  /*先使用浏览器自带字体显示文本，直到自定义字体下载完成*/
  font-family: "SourceHanSans-Nm", "Microsoft YaHei", 微软雅黑, Helvetica;
  /* 字体抗锯齿*/
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
/*自定义颜色*/
#nprogress .bar {
  background: var(--el-color-primary) !important;
}
/*更改颜色后会发现进度条头部还存在默认颜色的阴影，用以下代码进行更改*/
#nprogress .peg {
  box-shadow: 0 0 10px red, 0 0 5px red !important;
}
</style>
